//
//  XJLChatViewController.m
//  XJL_WeChat
//
//  Created by XJL on 16/8/6.
//  Copyright © 2016年 Xujiali. All rights reserved.
//

#import "XJLChatViewController.h"
#import "XJLMessageViewCell.h"

@interface XJLChatViewController ()<UITextViewDelegate,UITableViewDelegate,UITableViewDataSource>
/**
 *InputToolBar 的底部约束
 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *inputToolBarBottomConst;
/**
 *InputToolBar 的高度约束
 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *inputToolBarHeightConst;

/** 数组存储数据*/
@property(nonatomic,strong) NSMutableArray *data;
@end

@implementation XJLChatViewController

-(NSMutableArray *)data{
    if (_data == nil) {
        _data = [NSMutableArray array];
    }
    return _data;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.data addObject:@"1234566底部约束"];
    [self.data addObject:@"1234214底部约束42134底部约束44底部约束42134底部约束44底部约束42134底部约束44底部约束42134底部约束44底部约束42134底部约束44底部约束42134底部约束434底部约束42134底部约束42134底部约束42134底部约束"];
    [self.data addObject:@"12342134底部2134底部2134底部2134底部2134底部2134底部约束"];
    [self.data addObject:@"12342134底部约134底部约134底部约134底部约134底部约134底部约束"];
    [self.data addObject:@"1234214底部约束42134底部约束44底部约束42134底部约束44底部约束42134底部约束44底部约束42134底部约束44底部约束42134底部约束44底部约束42134底部约束434底部约束42134底部约束42134底部约束42134底部约束"];
    [self.data addObject:@"12342134底部2134底部2134底部2134底部2134底部2134底部约束"];
    [self.data addObject:@"12342134底部约134底部约134底部约134底部约134底部约134底部约束"];
    // 2.更改InputToolBar 的底部约束
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    //监听弹出键盘，隐藏事件
    [self setupKeyBoardNotification];
    
}

-(void)viewWillDisappear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

-(void)setupKeyBoardNotification{
    //弹出键盘
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    //隐藏事件
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillShowNotification object:nil];
}

-(void)keyBoardWillShow:(NSNotification *)note{
    // 1.获取键盘的高度
    CGRect kbEndFrm = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat kbHeight = kbEndFrm.size.height;
    
    XJLLog(@"%f",kbHeight);
    self.inputToolBarBottomConst.constant = kbHeight;
    
}

-(void)keyBoardWillHide:(NSNotification *)note{
    self.inputToolBarBottomConst.constant = 0;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillShowNotification object:nil];
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
//    self.inputToolBarHeightConst.constant = 44;
}

#pragma mark - 监听文本内容的改变
-(void)textViewDidChange:(UITextView *)textView{
    // 1.更改InputToolBar的高度
    
    // 1.1计算文字所需要的高度
    CGFloat textHeight = textView.contentSize.height;
    XJLLog(@"%f",textHeight);
    
    if (textHeight > 68) {
        textHeight = 67;
    }
    
    // 1.1更改InputToolBar的高度
    self.inputToolBarHeightConst.constant = 5 + textHeight + 6;
    
    // 2.判断send按钮,其实就是判断最后一个字符是否为换行字符(\n)
    if ([textView.text hasSuffix:@"\n"]) {
        XJLLog(@"Send按钮事件");
        //发送聊天消息给服务器
        //把内容清空
        textView.text = nil;
        self.inputToolBarHeightConst.constant = 44;
        
    }
    
}

#pragma mark 表格的数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.data.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    XJLMessageViewCell *cell = nil;
    static NSString *SenderID = @"SenderCell";
    static NSString *ReceiverID = @"ReceiverCell";
    if (indexPath.row % 2 == 0) {//发送方
        cell = [tableView dequeueReusableCellWithIdentifier:SenderID];
        
    }else{//接收方
        cell = [tableView dequeueReusableCellWithIdentifier:ReceiverID];
    
    }
    cell.messageLabel.text = self.data[indexPath.row];
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //获取文字
    NSString *text = self.data[indexPath.row];
    
    //计算高度
    CGFloat textHeight = [text boundingRectWithSize:CGSizeMake(240, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size.height;
    
    return 25 + textHeight + 25;
    
}

@end
